package cc.willrock.algorithm.sort;

import java.util.Arrays;

public class SelectSort {

	/*
	 * 最好O(n*n) 平均O(n*n) 最坏O(n×n)
	 */

	public static void selectMax(int[] a, int range) {
		int maxIndex = 0;
		for (int i = 0; i <= range; i++) {
			if (a[maxIndex] <= a[i]) {
				maxIndex = i;
			}
		}
		int max = a[maxIndex];
		for (int j = maxIndex; j < a.length - 1; j++) {
			a[j] = a[j + 1];
		}
		a[a.length - 1] = max;
		return;

	}

	public static int[] sort(int a[]) {
		for (int i = 1; i <= a.length; i++) {
			selectMax(a, a.length - i);
		}
		return a;
	}

	public static void main(String[] args) {
		int[] a = { 15, 9, 8, 1, 4, 11, 7, 12, 13, 6, 5, 3, 16, 2, 10, 14 };
		// int[] a = { 5, 3, 1, 2, 4 };
		System.out.println(Arrays.toString(a));
		System.out.println(Arrays.toString(sort(a)));
	}

}
